package com.bsj.travel.mqtt.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import net.dreamlu.iot.mqtt.core.client.IMqttClientConnectListener;
import org.tio.core.ChannelContext;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yinhao
 * @version 1.0
 * @description
 * @date 2024/4/12
 */
public class MqttClient {

    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newFixedThreadPool(100);
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("F:\\下载文件\\google文件下载\\MQTT连接参数.xlsx"));
//        reader.read()
        for (int i = 0; i < 10; i++) {
//            executorService.submit(new MqttTask());
        }
    }

    static class MqttTask implements Runnable{
        String username;
        String password;
        String clientId;
        public MqttTask(String username, String password, String clientId){
            this.username = username;
            this.password = password;
            this.clientId = clientId;
        }

        @Override
        public void run() {
            connect(username, password, clientId);
        }
    }


    public static void connect(String username, String password, String clientId) {
        net.dreamlu.iot.mqtt.core.client.MqttClient client = net.dreamlu.iot.mqtt.core.client.MqttClient.create()
                .ip("106.55.223.104")
                .port(1883)
                .username(username)
                .password(password)
                .clientId(clientId)
                .connectListener(new MqttListener())
                .connectSync();
//                .willMessage(builder -> {})
    }
    static class MqttListener implements IMqttClientConnectListener {

        @Override
        public void onConnected(ChannelContext context, boolean isReconnect) {
            if (isReconnect) {
                System.out.println("重连成功");
            } else {
                System.out.println("服务器连接成功");
            }
        }

        @Override
        public void onDisconnect(ChannelContext context, Throwable throwable,
                                 String remark, boolean isRemove) {
            System.out.println("mqtt 连接断开 remark： " + remark + "|isRemove: " + isRemove);
        }
    }
}
